--------------------------------------------------------------------------------------------
------ 常用函数放 TUNING 里
--------------------------------------------------------------------------------------------
----- RPC 命名空间
TUNING["hutao.RPC_NAMESPACE"] = "hutao_RPC"


--------------------------------------------------------------------------------------------

TUNING["hutao.fn"] = {}
TUNING["hutao.fn"].GetStringsTable = function(prefab_name)
    -------- 读取文本表
    -------- 如果没有当前语言的问题，调取中文的那个过去
    -------- 节省重复调用运算处理
    if TUNING["hutao.fn"].GetStringsTable_last_prefab_name == prefab_name then
        return TUNING["hutao.fn"].GetStringsTable_last_table or {}
    end


    local LANGUAGE = "ch"
    if type(TUNING["hutao.Language"]) == "function" then
        LANGUAGE = TUNING["hutao.Language"]()
    elseif type(TUNING["hutao.Language"]) == "string" then
        LANGUAGE = TUNING["hutao.Language"]
    end
    local ret_table = prefab_name and TUNING["hutao.Strings"][LANGUAGE] and TUNING["hutao.Strings"][LANGUAGE][tostring(prefab_name)] or nil
    if ret_table == nil and prefab_name ~= nil then
        ret_table = TUNING["hutao.Strings"]["ch"][tostring(prefab_name)]
    end

    ret_table = ret_table or {}
    TUNING["hutao.fn"].GetStringsTable_last_prefab_name = prefab_name
    TUNING["hutao.fn"].GetStringsTable_last_table = ret_table

    return ret_table
end
--------------------------------------------------------------------------------------------

TUNING.HUTAO_FNS = {}
--------------------------------------------------------------------------------------------
--- 文本参数表
    function TUNING.HUTAO_FNS:GetString(prefab,index)
        local ret_table = TUNING["hutao.fn"].GetStringsTable(prefab) or {}
        if index then
            return ret_table[index]
        else
            return ret_table
        end
    end
--------------------------------------------------------------------------------------------
--- 台词本
    function TUNING.HUTAO_FNS:GetDialogue(index)
        local ret = self:GetString("hutao_dialogue_script",index)
        if type(ret) == "string" or ret == nil then
            return ret
        elseif type(ret) == "table" then
            return ret[math.random(#ret)]
        end
        return nil
    end
--------------------------------------------------------------------------------------------
-- 热键
    local keys_by_index  = {
        KEY_A = 97,
        KEY_B = 98,
        KEY_C = 99,
        KEY_D = 100,
        KEY_E = 101,
        KEY_F = 102,
        KEY_G = 103,
        KEY_H = 104,
        KEY_I = 105,
        KEY_J = 106,
        KEY_K = 107,
        KEY_L = 108,
        KEY_M = 109,
        KEY_N = 110,
        KEY_O = 111,
        KEY_P = 112,
        KEY_Q = 113,
        KEY_R = 114,
        KEY_S = 115,
        KEY_T = 116,
        KEY_U = 117,
        KEY_V = 118,
        KEY_W = 119,
        KEY_X = 120,
        KEY_Y = 121,
        KEY_Z = 122,
        KEY_F1 = 282,
        KEY_F2 = 283,
        KEY_F3 = 284,
        KEY_F4 = 285,
        KEY_F5 = 286,
        KEY_F6 = 287,
        KEY_F7 = 288,
        KEY_F8 = 289,
        KEY_F9 = 290,
        KEY_F10 = 291,
        KEY_F11 = 292,
        KEY_F12 = 293,
    }
    local function check_is_text_inputting()    
        -- 代码来自  TheFrontEnd:OnTextInput
        local screen = TheFrontEnd and TheFrontEnd:GetActiveScreen()
        if screen ~= nil then
            if TheFrontEnd.forceProcessText and TheFrontEnd.textProcessorWidget ~= nil then
                return true
            else
                return false
            end
        end
        return false
    end
    function TUNING.HUTAO_FNS:IsKeyPressed(str_index,key)
        if check_is_text_inputting() then
            return false
        end
        if key == keys_by_index[str_index] then
            return true
        end
        return false
    end
--------------------------------------------------------------------------------------------
-- 最远可通过距离
    local function temp_passable_fn(x,y,z)
        return TheWorld.Map:IsPassableAtPoint(x, y, z)
    end
    function TUNING.HUTAO_FNS:GetMaxPassablePoint(start_pt, end_pt, max_dist, delta_search_distance,point_check_fn)
        --------------------------------------------------------------------
        --- 参数表初始化
            local MAX_DISTANCE = max_dist or 10
            delta_search_distance = delta_search_distance or 0.2
            point_check_fn = point_check_fn or temp_passable_fn
        --------------------------------------------------------------------
        -- 归一化方向向量
        local Normalize_Vect = (end_pt - start_pt):Normalize()        
        -- 计算两点之间的距离
        local total_distance = math.min((end_pt - start_pt):Length(), MAX_DISTANCE)
        -- 从最远端开始，逐步向前检查
        for i = total_distance, 0, -delta_search_distance do
            local pt = start_pt + Normalize_Vect * i
            if point_check_fn(pt.x,0,pt.z) then
                -- 找到第一个可通过的点即返回
                return pt
            end
        end        
        -- 如果没有找到任何可通过的点，则返回起点
        return start_pt
    end
--------------------------------------------------------------------------------------------
--- 获取一圈坐标
    function TUNING.HUTAO_FNS:GetSurroundPoints(CMD_TABLE)
        -- local CMD_TABLE = {
        --     target = inst or Vector3(),
        --     range = 8,
        --     num = 8
        -- }
        if CMD_TABLE == nil then
            return
        end
        if CMD_TABLE.pt then
            CMD_TABLE.target = CMD_TABLE.pt
        end
        local theMid = nil
        if CMD_TABLE.target == nil then
            theMid = Vector3( self.inst.Transform:GetWorldPosition() )
        elseif CMD_TABLE.target.x then
            theMid = CMD_TABLE.target
        elseif CMD_TABLE.target.prefab then
            theMid = Vector3( CMD_TABLE.target.Transform:GetWorldPosition() )
        else
            return
        end
        -- --------------------------------------------------------------------------------------------------------------------
        -- -- 8 points
        -- local retPoints = {}
        -- for i = 1, 8, 1 do
        --     local tempDeg = (PI/4)*(i-1)
        --     local tempPoint = theMidPoint + Vector3( Range*math.cos(tempDeg) ,  0  ,  Range*math.sin(tempDeg)    )
        --     table.insert(retPoints,tempPoint)
        -- end
        -- --------------------------------------------------------------------------------------------------------------------
        local num = CMD_TABLE.num or 8
        local range = CMD_TABLE.range or 8
        local retPoints = {}
        for i = 1, num, 1 do
            local tempDeg = (2*PI/num)*(i-1)
            local tempPoint = theMid + Vector3( range*math.cos(tempDeg) ,  0  ,  range*math.sin(tempDeg)    )
            table.insert(retPoints,tempPoint)
        end

        return retPoints


    end
--------------------------------------------------------------------------------------------
